//
//  NSDateFormatter+BZHelper.m
//  BZFoundation
//
//  Created by 郭艺伟 on 2017/10/1.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "NSDictionary+BZHelper.h"
#import "NSArray-BZFoundation.h"
#import "NSString+BZCreated.h"
@implementation NSDictionary (BZHelper)
//是否包含了指定KEY的对象
- (BOOL)bz_containObject:(id)key {
    BOOL bContain = NO;
    if (key) {
        if ([self objectForKey:key]) {
            bContain = YES;
        }
    }
    
    return bContain;
}

- (id)bz_safeObjectForKey:(id)aKey {
    
    id object = [self objectForKey:aKey];
    
    if (object && [object isKindOfClass:[NSNull class]]) {
        
        NSArray *array = [NSArray bz_parentCallerSymbolArray];
        
        NSLog(@"<:%p %@%@> call:%@ occur NSNull object", self, NSStringFromSelector(_cmd), aKey,
                   [NSString bz_stringByCallstackSymbols:array]);
        return nil;
    }
    
    return object;
}

- (id)bz_safeObjectForKey:(id)aKey default:(id)defaultValue {
    id object = [self bz_safeObjectForKey:aKey];
    if (!object) {
        object = defaultValue;
    }
    
    return object;
}

- (id)bz_safeNumberObjectForKey:(id)aKey {
    id object = [self bz_safeObjectForKey:aKey];
    if (object && ![object isKindOfClass:[NSNumber class]]) {
        if ([object isKindOfClass:[NSString class]]) {
            NSNumberFormatter *formate = [[NSNumberFormatter alloc] init];
            [formate setNumberStyle:NSNumberFormatterDecimalStyle];
            object = [formate numberFromString:object];
        } else {
            object = nil;
        }
    }
    return object;
}

- (id)bz_safeStringObjectForKey:(id)aKey {
    id object = [self bz_safeObjectForKey:aKey];
    if (object && ![object isKindOfClass:[NSString class]]) {
        if ([object isKindOfClass:[NSNumber class]]) {
            object = [object stringValue];
        } else {
            object = nil;
        }
    }
    return object;
}

@end
